package com.hengpeng.itfints.service.article.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.ArticleCategory;
import com.hengpeng.itfinbase.persist.ArticleGroupEx;
import com.hengpeng.itfints.service.BaseService;
import com.hengpeng.itfints.service.article.ArticleService;


@Service
public class ArticleServiceImpl extends BaseService implements ArticleService
{
	/**
	 * 按分组查询
	 */
	@Override
	public QueryResult<Article> findArticleListByGroup(Long groupId, Integer count, Integer currentPage)
	{
		DynamicQuery query = new DynamicQuery(Article.class, "a");
		query.createAlias(ArticleGroupEx.class, "age");
		query.eq("age.groupId", groupId);
		query.eqProperty("age.articleId", "a.id");
		query.addProjection(Projections.property("a"));
		query.setPageSize(count);
		query.setPageIndex(currentPage);
		query.desc("a.publishTime");
		query.setIsSearchTotalCount(Boolean.TRUE);
		return portFactory.remoteQuery(query, Article.class);
	}
	public Article getArticle(Long articleId)
	{
		return serviceFactory.getPersistService().get(Article.class, articleId);
	}
	
	public List<Article> findArticleListByGroupId(Long groupId, Integer count)
	{
		DynamicQuery query = new DynamicQuery(Article.class, "a");
		query.createAlias(ArticleGroupEx.class, "age");
		query.eq("age.groupId", groupId);
		query.eqProperty("age.articleId", "a.id");
		query.addProjection(Projections.property("a.*"));
		query.setPageSize(count);
		query.desc("a.publishTime");
		query.setQueryType(QueryType.JDBC);
		return portFactory.remoteQuery(query, Article.class).getResults();
	}
	
	public QueryResult<Article> findArticleByCategoryId(Long categoryId, Integer count, Integer currentPage)
	{
		DynamicQuery query = new DynamicQuery(Article.class);
		query.eq("category.id", categoryId);
		query.asc("id");
		query.setPageSize(count);
		query.setPageIndex(currentPage);
		return portFactory.remoteQuery(query, Article.class);
	}
	
	public List<ArticleCategory> findArticleCategoryAll(){
		DynamicQuery query = new DynamicQuery(ArticleCategory.class);
		query.asc("id");
		return serviceFactory.getPersistService().find(ArticleCategory.class, query);
	}
	@Override
	public ServiceResult<List<Article>> queryList(Integer pageIndex,
			Integer pageSize) {
		DynamicQuery query = new DynamicQuery(Article.class);
		query.desc("publishTime"); 
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.setQueryType(QueryType.JDBC);
		query.setIsSearchTotalCount(true);
		QueryResult<Article> requery = serviceFactory.getPersistService().findWithPage(Article.class, query);
		Long count = requery.getTotalCount();
		ServiceResult<List<Article>> serviceResult = new ServiceResult<List<Article>>();// 设置返回值
		serviceResult.setReturnValue(requery.getResults());
		serviceResult.setReturnListSize(count.intValue());
		return serviceResult;
	}
	
}
